<?php

namespace App\Repositories;

use App\Models\Task;
use Illuminate\Support\Facades\Auth;

/**
 * 上线单仓库
 */
class TaskRepository
{
    /**
     * 获取数据
     * @return array
     */
    public function getPageList()
    {
        $title = request('title', '');
        $projectId = request('project_id', '');

        $task = new Task();

        //相关用户项目数据
        $task = $task->where("user_id", Auth::id());

        // 上线单标题搜索
        if ($title) {
            $task = $task->where('title', 'like', "%" . $title . "%");
        }
        // 项目ID
        if ($projectId) {
            $task = $task->where('project_id', $projectId);
        }
        $task = $task->orderBy('id', "desc");

        return $task->paginate(config("global.paginate"));
    }


    /**
     * 添加上线单
     * @param $request
     * @return bool
     */
    public function store($request)
    {
        $task = new Task();
        $task->user_id = Auth::id();
        if ($task->fill($request->all())->save()) {
            flash(trans('alerts.task.created_success'))->success();
            return true;
        }
        flash(trans('alerts.task.created_error'))->error();
        return false;
    }

    /**
     * 修改上线单视图
     * @param $id
     * @return array
     */
    public function edit($id)
    {
        $task = Task::find($id);
        if ($task) {
            return $task;
        }
        abort(404);
    }

    /**
     * 修改上线单资料
     * @param $request
     * @param $id
     * @return bool
     */
    public function update($request, $id)
    {
        $task = Task::find($id);
        if ($task) {
            if ($task->fill($request->all())->save()) {
                flash(trans('alerts.task.updated_success'))->success();
                return true;
            }
            flash(trans('alerts.task.updated_error'))->error();
            return false;
        }
        abort(404);
    }

    /**
     * 删除角色
     * @param $id
     * @return bool
     */
    public function destroy($id)
    {
        try {
            $task = Task::find($id);
            if (!$task) {
                throw new \Exception(trans('alerts.task.unknown_deployment_bill'));
            }
            if ($task->user_id != Auth::id()) {
                throw new \Exception(trans('alerts.task.you_are_not_the_member_of_project'));
            }
            if ($task->status == config("constants.task.status.complete")) {
                throw new \Exception(trans('alerts.task.can_not_delete_the_job_which_is_done'));
            }
            if (!Task::destroy($id)) {
                throw new \Exception(trans('alerts.task.deleted_error'));
            }
            flash(trans('alerts.task.deleted_success'))->success();
            return true;
        } catch (\Exception $e) {
            flash($e->getMessage())->error();
            return false;
        }
    }

    /**
     * 是否能进行部署
     *
     * @param $status
     * @return bool
     */
    public static function canDeploy($status)
    {
        return in_array($status, [config("constants.task.status.new"), config("constants.task.status.fail")]);
    }

    /**
     * 获取项目上线单数量
     *
     * @param $projectId
     * @return bool
     */
    public static function projectTaskCount($projectId){
        return Task::where("project_id", $projectId)->get()->count();
    }
}